package zpy.servlet.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import zpy.servlet.pojo.Student;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet implementation class SessionSerlet
 */
@WebServlet("/SessionSerlet")
public class SessionSerlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SessionSerlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session1 = request.getSession();
		HttpSession session2 = request.getSession();
		
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.println("session1的id = "+session1.getId());
		writer.println("session2的id = "+session2.getId());
		if (session1 == session2) {
			writer.println("session1与session2是同一个会话");
		}else{
			writer.println("session1与session2不是同一个会话");
		}
		Cookie[] cookies =  request.getCookies();
		for(Cookie cookie : cookies){
			writer.println(cookie.getName() + "-->" + cookie.getValue());
		}
		
		Student student = (Student) session1.getAttribute("student");
		writer.println("用户名:"+student.getName());
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
